package com.kwxy.hyt.controller;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.kwxy.hyt.base.BaseController;
import com.kwxy.hyt.po.*;
import com.kwxy.hyt.utils.Pager;
import java.util.*;

import com.kwxy.hyt.po.*;
import com.kwxy.hyt.mapper.*;
import com.kwxy.hyt.service.*;

/**
 * @ClassName:  
 * @Description: 
 * @author  - - admin
 * @date - 2020年9月10日 09时48分24秒
 */


@Controller
@RequestMapping("/sign")
public class SignController extends BaseController {
	
	/**
	 * 依赖注入 start dao/service/===
	 */
	@Autowired
	private RecordService recordService;
	
	@Autowired
	private SignService signService;
	
	
	@Autowired
	private TeacherBjService teacherBjService;
	
	@Autowired
	private FdyService fdyService;
	
	@Autowired
	private StudentService studentService;
	
	/**
	 * 教师查看自己所教学的班级
	 * mybj
	 */
	
	@RequestMapping(value = "/mybj")
	public String mybj(Sign sign, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		Object attribute = request.getSession().getAttribute("userId");
		
		if (attribute == null){
			return "redirect:/login/uLogin";	
		}
		Integer userId = Integer.valueOf(attribute.toString());
		TeacherBj b = new TeacherBj();
		b.setTeacherId(userId);
		List<TeacherBj> listAllByEntity = teacherBjService.listAllByEntity(b);
		model.addAttribute("bjs", listAllByEntity);
		return "sign/mybj";
	}
	
	@RequestMapping(value = "/mybj2")
	public String mybj2(Sign sign, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		Object attribute = request.getSession().getAttribute("userId");
		
		if (attribute == null){
			return "redirect:/login/uLogin";	
		}
		Integer userId = Integer.valueOf(attribute.toString());
		TeacherBj b = new TeacherBj();
		b.setTeacherId(userId);
		List<TeacherBj> listAllByEntity = teacherBjService.listAllByEntity(b);
		model.addAttribute("bjs", listAllByEntity);
		return "sign/mybj2";
	}
	
	
	//${ctx}/sign/mySign.do?bjId=${data.bjId}
	
	@RequestMapping(value = "/findBySql")
	public String findBySql(Sign sign, Model model, HttpServletRequest request, HttpServletResponse response) {
		//分页查询
		Object attribute = request.getSession().getAttribute("userId");
		
		if (attribute == null){
			return "redirect:/login/uLogin";	
		}
		Integer userId = Integer.valueOf(attribute.toString());
		
		String sql = "SELECT * FROM sign WHERE bjId= "+sign.getBjId() + " and teacherId="+userId;
        if(!isEmpty(sign.getBjId())){
        	sql += " and bjId like '%"+sign.getBjId()+"%'";
		}
        if(!isEmpty(sign.getTeacherId())){
        	sql += " and teacherId like '%"+sign.getTeacherId()+"%'";
		}
        if(!isEmpty(sign.getName())){
        	sql += " and name like '%"+sign.getName()+"%'";
		}
        if(!isEmpty(sign.getStatus())){
        	sql += " and status like '%"+sign.getStatus()+"%'";
		}
        if(!isEmpty(sign.getFdyId())){
        	sql += " and fdyId like '%"+sign.getFdyId()+"%'";
		}
       sql += " ORDER BY ID DESC ";
		Pager<Sign> pagers = signService.findBySqlRerturnEntity(sql);
		model.addAttribute("pagers", pagers);
		//存储查询条件
		model.addAttribute("obj", sign);
		model.addAttribute("bjId", sign.getBjId());
		return "sign/sign";
	}
	
	/**
	 * 辅导员列表
	 * @param sign
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/fdySign")
	public String fdySign(Sign sign, Model model, HttpServletRequest request, HttpServletResponse response) {
		//分页查询
		Object attribute = request.getSession().getAttribute("userId");
		
		if (attribute == null){
			return "redirect:/login/uLogin";	
		}
		Integer userId = Integer.valueOf(attribute.toString());
		
		String sql = "SELECT * FROM sign WHERE fdyId= "+userId;
		
        if(!isEmpty(sign.getBjId())){
        	sql += " and bjId like '%"+sign.getBjId()+"%'";
		}
        if(!isEmpty(sign.getTeacherId())){
        	sql += " and teacherId like '%"+sign.getTeacherId()+"%'";
		}
        if(!isEmpty(sign.getName())){
        	sql += " and name like '%"+sign.getName()+"%'";
		}
        if(!isEmpty(sign.getStatus())){
        	sql += " and status like '%"+sign.getStatus()+"%'";
		}
        if(!isEmpty(sign.getFdyId())){
        	sql += " and fdyId like '%"+sign.getFdyId()+"%'";
		}
       sql += " ORDER BY ID DESC ";
		Pager<Sign> pagers = signService.findBySqlRerturnEntity(sql);
		model.addAttribute("pagers", pagers);
		//存储查询条件
		model.addAttribute("obj", sign);
		return "sign/fdySign";
	}
	
	
	
	
	/**
	 * 分页查询 返回list对象(通过Map)
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/findByMap")
	public String findByMap(Sign sign, Model model, HttpServletRequest request, HttpServletResponse response) {
		//通过map查询
		Map<String,Object> params = new HashMap<String,Object>();
        if(!isEmpty(sign.getBjId())){
        	params.put("bjId", sign.getBjId());
		}
        if(!isEmpty(sign.getTeacherId())){
        	params.put("teacherId", sign.getTeacherId());
		}
        if(!isEmpty(sign.getName())){
        	params.put("name", sign.getName());
		}
        if(!isEmpty(sign.getStatus())){
        	params.put("status", sign.getStatus());
		}
        if(!isEmpty(sign.getFdyId())){
        	params.put("fdyId", sign.getFdyId());
		}
		//分页查询
		Pager<Sign> pagers = signService.findByMap(params);
		model.addAttribute("pagers", pagers);
		//存储查询条件
		model.addAttribute("obj", sign);
		return "sign/sign";
	}
	
	/**********************************【增删改】******************************************************/
	
	/**
	 * 跳至添加页面
	 * @return
	 */
	@RequestMapping(value = "/add")
	public String add(Integer bjId,Model model) {
		model.addAttribute("bjId", bjId);
		return "sign/add";
	}

	/**
	 * 跳至详情页面
	 * @return
	 */
	@RequestMapping(value = "/view")
	public String view(Integer id,Model model) {
		Sign obj = signService.load(id);
		model.addAttribute("obj",obj);
		return "sign/view";
	}
	
	/**
	 * 添加执行
	 * @return
	 */
	@RequestMapping(value = "/exAdd")
	public String exAdd(Sign sign, Model model, HttpServletRequest request, HttpServletResponse response) {
	Object attribute = request.getSession().getAttribute("userId");
		
		if (attribute == null){
			return "redirect:/login/uLogin";	
		}
		Integer userId = Integer.valueOf(attribute.toString());
		sign.setTeacherId(userId);
		sign.setStatus(0);
		signService.insert(sign);
		//查询学生，插入记录表
		Student s = new Student();
		s.setBjId(sign.getBjId());
		s.setIsDelete(0);
		List<Student> listAllByEntity = studentService.listAllByEntity(s);
		if (!CollectionUtils.isEmpty(listAllByEntity)){
			
			for (Student ss : listAllByEntity){
				Record re = new Record();
			//	re.setCreateTime(new Date());
				re.setSignId(sign.getId());
				//0 未签到 1 已签到 2 请假
				re.setStatus(0);
				re.setStudentId(ss.getId());
				recordService.insert(re);
			}
		}
		return "redirect:/sign/findBySql.action?bjId="+sign.getBjId();
	}
	
	
	/**
	 * 跳至修改页面
	 * @return
	 */
	@RequestMapping(value = "/update")
	public String update(Integer id,Model model) {
		Sign obj = signService.load(id);
		model.addAttribute("obj",obj);
		return "sign/update";
	}
	
	/**
	 * 添加修改
	 * @return
	 */
	@RequestMapping(value = "/sp")
	public String sp(Sign sign, Model model, HttpServletRequest request, HttpServletResponse response) {
		//1.通过实体类修改，可以多传修改条件
		Sign obj = signService.load(sign.getId());
		System.out.println("model===="+model);
		System.out.println("sign===="+sign);
		if (sign.getStatus() == 2){
			Fdy f = new Fdy();
			f.setBjId(obj.getBjId());
			Fdy byEntity = fdyService.listAllByEntity(f).get(0);
			obj.setFdyId(byEntity.getId());
		}
		obj.setStatus(sign.getStatus());
		signService.updateById(obj);
		Record record = new Record();
		record.setSignId(sign.getId());
		record.setStatus(0);
		Student student = new Student();
		for (Record record1 : recordService.listAllByEntity(record)) {
			student.setId(record1.getStudentId());
			student.setCount(studentService.getById(record1.getStudentId()).getCount()+1);
			studentService.updateById(student);
		}
//		studentService.updateById()
		//2.通过主键id修改
		//signService.updateById(sign);
		return "redirect:/sign/findBySql.action?bjId="+obj.getBjId();
	}
	
	@RequestMapping(value = "/exUpdate")
	public String exUpdate(Sign sign, Model model, HttpServletRequest request, HttpServletResponse response) {
		Sign obj = signService.load(sign.getId());
		signService.updateById(sign);
		return "redirect:/sign/findBySql?bjId="+obj.getBjId();
	}
	
	/**
	 * 删除通过主键
	 * @return
	 */
	@RequestMapping(value = "/delete")
	public String delete(Integer id, Model model, HttpServletRequest request, HttpServletResponse response) {
		Sign obj = signService.load(id);
		///1.通过主键删除
		signService.deleteById(id);
		return "redirect:/sign/findBySql?bjId="+obj.getBjId();
	}
	
	
}
